package z_whiteboard2;
import java.awt.Frame;
import java.awt.Dimension;
import java.awt.TextField;
import java.awt.Rectangle;
import java.awt.Button;
import java.awt.List;
import java.awt.TextArea;

import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ObjectMultiClient extends Frame
		implements Runnable, ActionListener{

	private static final long serialVersionUID = 1L;
	public TextField userID = null;
	private TextField serverIP = null;
	private TextField serverPort = null;
	private Button connectBtn = null;
	private Button disconnectBtn = null;
	private Button whiteBtn = null;
	private List userList = null;
	private TextArea doc = null;
	private TextField talk = null;
	private Button sendBtn = null;

	Socket s = null;
	ObjectInputStream receive = null;
	ObjectOutputStream send = null;
	MyPainter mp = null;
	boolean threadFlag=true;

	private TextField getUserID() {
		if (userID == null) {
			userID = new TextField("park");
			userID.setBounds(new Rectangle(23, 81, 220, 24));
		}
		return userID;
	}

	/**
	 * This method initializes serverIP
	 *
	 * @return java.awt.TextField
	 */
	private TextField getServerIP() {
		if (serverIP == null) {
			serverIP = new TextField("127.0.0.1");
			serverIP.setBounds(new Rectangle(23, 51, 131, 23));
		}
		return serverIP;
	}

	/**
	 * This method initializes serverPort
	 *
	 * @return java.awt.TextField
	 */
	private TextField getServerPort() {
		if (serverPort == null) {
			serverPort = new TextField("11111");
			serverPort.setBounds(new Rectangle(160, 51, 84, 21));
		}
		return serverPort;
	}

	/**
	 * This method initializes connectBtn
	 *
	 * @return java.awt.Button
	 */
	private Button getConnectBtn() {
		if (connectBtn == null) {
			connectBtn = new Button("연결");
			connectBtn.setBounds(new Rectangle(252, 53, 81, 56));
			connectBtn.addActionListener(this);
		}
		return connectBtn;
	}

	/**
	 * This method initializes disconnectBtn
	 *
	 * @return java.awt.Button
	 */
	private Button getDisconnectBtn() {
		if (disconnectBtn == null) {
			disconnectBtn = new Button("연결종료");
			disconnectBtn.setBounds(new Rectangle(342, 55, 83, 55));
			disconnectBtn.addActionListener(this);
		}
		return disconnectBtn;
	}

	/**
	 * This method initializes whiteBtn
	 *
	 * @return java.awt.Button
	 */
	private Button getWhiteBtn() {
		if (whiteBtn == null) {
			whiteBtn = new Button("화이트보드");
			whiteBtn.setBounds(new Rectangle(431, 56, 76, 57));
			whiteBtn.addActionListener(this);
		}
		return whiteBtn;
	}

	/**
	 * This method initializes userList
	 *
	 * @return java.awt.List
	 */
	private List getUserList() {
		if (userList == null) {
			userList = new List();
			userList.setBounds(new Rectangle(33, 115, 147, 204));
		}
		return userList;
	}

	/**
	 * This method initializes doc
	 *
	 * @return java.awt.TextArea
	 */
	private TextArea getDoc() {
		if (doc == null) {
			doc = new TextArea();
			doc.setBounds(new Rectangle(189, 117, 320, 202));
		}
		return doc;
	}

	/**
	 * This method initializes talk
	 *
	 * @return java.awt.TextField
	 */
	private TextField getTalk() {
		if (talk == null) {
			talk = new TextField();
			talk.setBounds(new Rectangle(35, 325, 349, 26));
		}
		return talk;
	}

	/**
	 * This method initializes sendBtn
	 *
	 * @return java.awt.Button
	 */
	private Button getSendBtn() {
		if (sendBtn == null) {
			sendBtn = new Button("전송");
			sendBtn.setBounds(new Rectangle(395, 325, 114, 29));
			sendBtn.addActionListener(this);
		}
		return sendBtn;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ObjectMultiClient omc = new ObjectMultiClient();

	}

	/**
	 * This is the default constructor
	 */
	public ObjectMultiClient() {
		super();
		initialize();
		this.setVisible(true);
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize() {
		this.setLayout(null);
		this.setSize(527, 369);
		this.setTitle("Frame");

		this.add(getUserID(), null);
		this.add(getServerIP(), null);
		this.add(getServerPort(), null);
		this.add(getConnectBtn(), null);
		this.add(getDisconnectBtn(), null);
		this.add(getWhiteBtn(), null);
		this.add(getUserList(), null);
		this.add(getDoc(), null);
		this.add(getTalk(), null);
		this.add(getSendBtn(), null);
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public void run(){
		try{
			VData d = null;
			while(threadFlag){
				d = (VData)receive.readObject();
				if(d.command.equals("login")){
					userList.removeAll();
					userList.add(d.user);	
				}
				
				if(d.command.equals("logins")){
					userList.add(d.user);	
				}

				if(d.command.equals("msg")){
					doc.append(d.msg + "\r\n");
				}
				
				if(d.command.equals("logout")){
					userList.remove(d.user);
				}
				
				if(d.command.equals("graphic")){
					if(mp == null) mp = new MyPainter(this);
					doc.append(d.user + "로 부터 그림 전송됨\r\n");
					mp.setData(d);
				}
				
				if(d.command.equals("shutdown")){
					threadFlag = false;
				}

				Thread.sleep(500);
			}
			
			receive.close();
			send.close();
			
				
		}catch(Exception ex){}
	}

	public void actionPerformed(ActionEvent e){
		if(e.getSource() == whiteBtn){
			if( mp == null){
				mp = new MyPainter(this);
			}
		}

		if(e.getSource() == connectBtn){
			try{
				s = new Socket(serverIP.getText(),
					Integer.parseInt(serverPort.getText()));

			OutputStream os = s.getOutputStream();
			send = new ObjectOutputStream(os);

			InputStream is = s.getInputStream();
			receive = new ObjectInputStream(is);

				new Thread(this).start();

				VData d = new VData();
				d.command="login";
				d.user = userID.getText();
				d.msg = " ] login ";
				sendMsg(d);

			}catch(Exception ex){}
		}

		if(e.getSource() == disconnectBtn){
			VData d = new VData();
			d.command = "logout";
			d.user=userID.getText();
			d.msg = " ] logout";
			sendMsg(d);

			threadFlag=false;

		}

		if(e.getSource() == sendBtn){
			VData d = new VData();
			d.command="msg";
			d.msg = userID.getText() + " ] " + talk.getText();
			sendMsg(d);
		}

	}

	public void sendMsg(VData d){
		try{
			send.writeObject(d);
			send.flush();
			System.out.println("send");
		}catch(Exception ex){}
	}


}  //  @jve:decl-index=0:visual-constraint="71,23"
